'''
Created on Sep 20, 2009

@author: mkiyer
'''

class Sample:    
    def __init__(self, name):
        self.name = name
        self.data = []

def read_expression_matrix(fhd):
    # first line is property header with sample names
    sample_group_header = fhd.readline()
    sample_groups = sample_group_header.strip().split('\t')[1:]
    sample_name_header = fhd.readline()
    sample_names = sample_name_header.strip().split('\t')[1:]
    samples = [Sample(x) for x in sample_names]
    # set sample properties
    while True:
        thisline = fhd.readline()
        thisline = thisline.strip()
        if len(thisline) == 0:
            break
        fields = thisline.split('\t')
        prop_name = fields[0]
        for i,prop_val in enumerate(fields[1:]):
            setattr(samples[i], prop_name, prop_val)
    # ignore for now?
    data_header = fhd.readline()
    # get sample data
    intervals = []
    for thisline in fhd:
        fields = thisline.strip().split('\t')
        chrom, interval = fields[0].split(':')
        start, end = map(int, interval.split('-'))
        intervals.append((chrom, start, end))
        for i, data_val in enumerate(fields[1:]):
            samples[i].data.append(float(data_val))    
    return intervals, samples

